package com.lechi.web.service.impl;

import com.lechi.web.common.enums.ErrorCodeEnum;
import com.lechi.web.common.exception.ServiceException;
import com.lechi.web.framework.log.IotExceptionLog;
import com.lechi.web.service.IIotSensorGroupService;
import com.lechi.web.system.domain.IotSensorGroupBo;
import com.lechi.web.system.mapper.iot.IotSensorGroupMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * 传感器分组Service业务层处理
 *
 * @author yangguang
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class IotSensorGroupServiceImpl implements IIotSensorGroupService {

    private final IotSensorGroupMapper iotSensorGroupMapper;

    /**
     * 查询传感器分组
     *
     * @param id 传感器分组主键
     * @return 传感器分组
     */
    @Override
    public IotSensorGroupBo selectIotSensorGroupById(Long id) {
        return iotSensorGroupMapper.selectIotSensorGroupById(id);
    }

    /**
     * 查询传感器分组列表
     *
     * @param iotSensorGroupBo 传感器分组
     * @return 传感器分组
     */
    @Override
    public List<IotSensorGroupBo> selectIotSensorGroupList(IotSensorGroupBo iotSensorGroupBo) {
        return iotSensorGroupMapper.selectIotSensorGroupList(iotSensorGroupBo);
    }

    /**
     * 新增传感器分组
     *
     * @param iotSensorGroupBo 传感器分组
     */
    @Override
    public void insertIotSensorGroup(IotSensorGroupBo iotSensorGroupBo) {
        iotSensorGroupBo.setCreateTime(new Date());
        try {
            iotSensorGroupMapper.insertIotSensorGroup(iotSensorGroupBo);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "新增传感器分组", method, iotSensorGroupBo));
            throw new ServiceException(ErrorCodeEnum.ADD_ERROR.getValue(), ErrorCodeEnum.ADD_ERROR.getKey());
        }
    }

    /**
     * 修改传感器分组
     *
     * @param iotSensorGroupBo 传感器分组
     */
    @Override
    public void updateIotSensorGroup(IotSensorGroupBo iotSensorGroupBo) {
        try {
            iotSensorGroupMapper.updateIotSensorGroup(iotSensorGroupBo);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "修改传感器分组", method, iotSensorGroupBo));
            throw new ServiceException(ErrorCodeEnum.EDIT_ERROR.getValue(), ErrorCodeEnum.EDIT_ERROR.getKey());
        }
    }

    /**
     * 删除传感器分组信息
     *
     * @param id 传感器分组主键
     */
    @Override
    public void deleteIotSensorGroupById(Long id) {
        try {
            iotSensorGroupMapper.deleteIotSensorGroupById(id);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "删除传感器分组", method, id));
            throw new ServiceException(ErrorCodeEnum.DELETE_ERROR.getValue(), ErrorCodeEnum.DELETE_ERROR.getKey());
        }
    }
}
